package com.wonderful.mall.coupon.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.wonderful.common.utils.R;
import com.wonderful.mall.coupon.entity.SeckillSkuRelationEntity;
import com.wonderful.mall.coupon.feign.ProductFeignService;
import com.wonderful.mall.coupon.service.SeckillSkuRelationService;
import com.wonderful.mall.coupon.vo.SecKillInfoVo;
import com.wonderful.mall.coupon.vo.SeckillSkuRedisTo;
import com.wonderful.mall.coupon.vo.SkuInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wonderful.common.utils.PageUtils;
import com.wonderful.common.utils.Query;

import com.wonderful.mall.coupon.dao.SeckillSessionDao;
import com.wonderful.mall.coupon.entity.SeckillSessionEntity;
import com.wonderful.mall.coupon.service.SeckillSessionService;

import javax.annotation.Resource;
import javax.xml.crypto.Data;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Resource
    SeckillSkuRelationService seckillSkuRelationService;

    @Resource
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLatest3DaysSession() {
        String now = LocalDateTime.of(LocalDate.now(), LocalTime.now()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().gt("end_time", now).lt("start_time", now));
        if (list != null && list.size() > 0) {
            return list.stream().peek(session -> {
                Long id = session.getId();
                session.setRelationEntities(seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id)));
            }).collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public List<SecKillInfoVo> getSecKillSku() {
        List<SecKillInfoVo> secKillInfoVos = new ArrayList<>();
        String now = LocalDateTime.of(LocalDate.now(), LocalTime.now()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List<SeckillSessionEntity> list = this.list(new QueryWrapper<SeckillSessionEntity>().gt("end_time", now).lt("start_time", now));
        if (list != null && list.size() > 0) {
            //正在秒杀的场次id
            List<Long> ids = list.stream().map(SeckillSessionEntity::getId).collect(Collectors.toList());
            for (Long id : ids) {
                List<SeckillSkuRelationEntity> relationEntities = seckillSkuRelationService.list(new QueryWrapper<SeckillSkuRelationEntity>().eq("promotion_session_id", id));
                for (SeckillSkuRelationEntity relationEntity : relationEntities) {
                    SecKillInfoVo secKillInfoVo = new SecKillInfoVo();
                    BeanUtils.copyProperties(relationEntity, secKillInfoVo);
                    Long skuId = secKillInfoVo.getSkuId();
                    R r = productFeignService.info(skuId);
                    if (r.getCode()==0){
                        SkuInfoVo skuInfo = r.getData("skuInfo", new TypeReference<SkuInfoVo>() {
                        });
                        secKillInfoVo.setSkuInfoVo(skuInfo);
                        secKillInfoVos.add(secKillInfoVo);
                    }
                }
            }
            return secKillInfoVos;
        }
        return null;
    }

    @Override
    public SeckillSkuRedisTo getSkuSecKillInfo(Long skuId) {
        SeckillSkuRedisTo seckillSkuRedisTo = new SeckillSkuRedisTo();
        SeckillSkuRelationEntity one = seckillSkuRelationService.getOne(new QueryWrapper<SeckillSkuRelationEntity>().eq("sku_id", skuId));
        if(one!=null){
            BeanUtils.copyProperties(one,seckillSkuRedisTo);
            Long sessionId = one.getPromotionSessionId();
            SeckillSessionEntity entity = this.getOne(new QueryWrapper<SeckillSessionEntity>().eq("id", sessionId));
            long startTime = entity.getStartTime().getTime();
            long endTime = entity.getEndTime().getTime();
            seckillSkuRedisTo.setStartTime(startTime);
            seckillSkuRedisTo.setEndTime(endTime);
            return seckillSkuRedisTo;
        }
        return null;
    }
}